/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package justdoit.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
import java.util.Locale;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import justdoit.Startup;
import justdoit.controller.TaskManager;
import justdoit.controller.LanguageManager;
import justdoit.model.User;
import justdoit.view.model.LanguagesComboBoxModel;

/**
 * The dialog for adding a new User
 * @author Can
 */
public class NewUserDialog extends JDialog {

    private JLabel userNameLabel;
    private JTextField userNameInput;
    private JLabel passwordLabel;
    private JLabel passwordLabel2;
    private JPasswordField passwordInput;
    private JPasswordField passwordInput2;
    private JLabel languageChooseLabel;
    private JComboBox languagesComboBox;
    private JLabel messageLabel;
    private JButton okButton;
    private JButton cancelButton;
    private JPanel inputPanel;
    private JPanel actionPanel;
    private final Integer width = 350;
    private final Integer height = 300;

    public NewUserDialog() {
        initComponents();
    }

    private void initComponents() {
        // input panel
        inputPanel = new JPanel();
        userNameLabel = new JLabel(LanguageManager.getString("label.login.username"));
        userNameLabel.setLabelFor(userNameInput);
        userNameInput = new JTextField(10);
        passwordLabel = new JLabel(LanguageManager.getString("label.login.password"));
        passwordLabel2 = new JLabel(LanguageManager.getString("label.login.confirmPassword"));
        passwordInput = new JPasswordField(10);
        passwordInput2 = new JPasswordField(10);
        languageChooseLabel = new JLabel(LanguageManager.getString("label.chooseLanguage"));
        languagesComboBox = new JComboBox(new LanguagesComboBoxModel(
                LanguageManager.getAvailableLocales()));
        languagesComboBox.setSelectedItem(TaskManager.getCurrentUser().getLocale().getDisplayName());
        languagesComboBox.setToolTipText(LanguageManager.getString("tooltip.language"));
        messageLabel = new JLabel();
        messageLabel.setForeground(Color.red);

        inputPanel.setLayout(new GridLayout(5, 2, 10, 10));
        inputPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        inputPanel.add(userNameLabel);
        inputPanel.add(userNameInput);
        inputPanel.add(passwordLabel);
        inputPanel.add(passwordInput);
        inputPanel.add(passwordLabel2);
        inputPanel.add(passwordInput2);
        inputPanel.add(languageChooseLabel);
        inputPanel.add(languagesComboBox);
        inputPanel.add(messageLabel);

        // action panel
        actionPanel = new JPanel();
        okButton = new JButton(LanguageManager.getString("button.ok"));
        cancelButton = new JButton(LanguageManager.getString("button.cancel"));

        okButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                okAction();
            }
        });

        cancelButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                cancelAction();
            }
        });

        actionPanel.setLayout(new FlowLayout());
        actionPanel.add(okButton);
        actionPanel.add(cancelButton);

        // the main panel
        this.setLayout(new BorderLayout());
        this.add(BorderLayout.CENTER, inputPanel);
        this.add(BorderLayout.SOUTH, actionPanel);

        this.getRootPane().setDefaultButton(okButton);

        this.setSize(width, height);
        this.setLocationRelativeTo(null);
        this.setTitle(LanguageManager.getString("title.addNewUser"));
        this.setResizable(false);
        this.setModal(true);
    }

    private void okAction() {
        if (!String.valueOf(passwordInput.getPassword()).equals(
                String.valueOf(passwordInput2.getPassword()))) {
            System.out.println("The password doesn't match!");
            // should be in English
            messageLabel.setText("Password doesn't match!");
            return;
        }
        if (TaskManager.addUser(getUserFromInterface()) == 0) {
            System.out.println("Add User Success!");
            this.dispose();
            Startup.getMainFrame().dispose();
            LanguageManager.loadLanguage(TaskManager.getCurrentUser().getLocale());
            Startup.showMainFrame(TaskManager.getCurrentUser());
            // Startup.getMainFrame().getTaskListTable().refreshData();
            // Startup.getMainFrame().updateUserNameLabel();
        } else {
            System.out.println("Add User Failed!");
        }
    }

    private void cancelAction() {
        this.dispose();
    }

    private User getUserFromInterface() {
        Locale locale = ((LanguagesComboBoxModel) languagesComboBox.getModel()).getSelectedLocale();
        if (locale == null) {
            System.out.println("Impossible!");
            System.exit(-1);
            return null;
        }
        User user = new User(userNameInput.getText(), locale);
        user.setPassword(passwordInput.getPassword());
        return user;
    }
}
